// 导入表单验证插件
const app = getApp()
import wxValidate from 'wechat-validate'
Page({
  data: {
    mobile: '',
    password:'',
  },
  // 将插件注入到页面实例中
  behaviors: [wxValidate],
  rules: {
    mobile: [
      {required: true, message: '请填写手机号码!'},
      {pattern: /^1[3-8]\d{9}$/, message: '请填写正确的手机号码!'}
    ],
    password:[
      {required: true, message: '请填写密码!'},
      {pattern: /^\w{6}$/, message: '请填写正确密码!'}
    ]
  },
  // valite(){
  //    const {message,valid} = this.validate('mobile')
  //    if(!valid) return wx.utils.toast(message)

  // },
  onLoad(options){
    this.returnUrl = options.returnUrl
 },
  async formSubmit(){
  
   console.log(!this.validate());
  if(!this.validate()) return wx.utils.toast('请输入正确信息')
    

  const res = await wx.http.post('/index/login',{
    mobile:this.data.mobile,
    password:this.data.password
 })
     console.log(res);
     if(res.errno !== 0) return wx.utils.toast('登录失败,请重新注册')

  app.setToken("token",res.data.token)
  wx.utils.toast('登录成功,即将跳转到首页')
  wx.reLaunch({
    url: '/pages/user/user',
  });

 
  }
})